package com.example.homewidgets

import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.os.Message
import android.os.Messenger
import androidx.annotation.RequiresApi


class MyWidgetService : Service() {

    private var replyTo: Messenger? = null

    // 在Service中创建Messenger
    private val mMessenger = Messenger(Handler { message ->
        // 处理接收到的消息
        if (replyTo == null && message.replyTo != null) {
            replyTo = message.replyTo
        }

        true
    })

    override fun onBind(intent: Intent?): IBinder? {
        return mMessenger.binder
    }

    @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent?.action == MyActions.CLICK_Widget2App) {
            // 在这里处理你的操作，例如发送通知到主app
            val msg = Message.obtain().apply {
                data = intent.extras
            }
            replyTo?.send(msg)
        }
        return super.onStartCommand(intent, flags, startId)
    }


}